from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from .models import *


def api_asset_add(request):

    '''
        @author:张
        @date:2018/10/11

        提交方式 ：POST
        提交数据格式  ：{'add_asset_name':'xxxx'}
        

        

    '''
    if request.method == 'POST':
        try:
            info = request.POST['add_asset_name'].strip()

        except KeyError:

            return HttpResponse('提交信息不对，或者键不是‘add_asset_name’')
            

        else:
            if info != '':
                try:
                    Asset.objects.create(asset_name = info)

                    return HttpResponse('添加成功')
                except django.db.utils.IntegrityError:#UNIQUE 报错

                    return HttpResponse(
                        '资产名（{}）已存在'.format(info))
            else:

                return HttpResponse('添加的资产名不能为空')
            
    elif request.method == 'GET':

        return HttpResponse('访问模式应该为POST')
                

    














#资产管理系统
def asset_manage(request):
        context = {'message':'欢迎来到本系统'}
        return render(request,'asset/asset_manage.html',context)

#资产添加
def function(request):

	return render(request,'asset/资产.html')

#资产添加运算
def asset(request):
	
	info = dict(request.POST)
	print(info)
	'''
                info = {'asset_name': ['×××'], 'asset_num': ['××']}
	'''
        

	return HttpResponse('ok')

#资产删除
def asset_del(request):
        context = {}
        return render(request,'asset/asset_del.html',context)

#资产删除运算
def asset_del_db(request):
        info = dict(request.POST)
        print(info)
        
        return HttpResponse('ok')

#资产查询
def asset_find(request):
        context = {}
        return render(request,'asset/asset_find.html',context)

#资产查询运算
def asset_find_db(request):
        info = dict(request.POST)
        print(info)
        return HttpResponse('ok')

#资产修改
def asset_alter(request):
        context = {}
        return render(request,'asset/asset_alter.html',context)

#资产修改运算
def asset_alter_db(request):
        info = dict(request.POST)
        print(info)
        return HttpResponse('ok')
